﻿<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBar" src="~/_controltemplates/ToolBar.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBarButton" src="~/_controltemplates/ToolBarButton.ascx" %>
<%@ Register TagPrefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="wssap" Namespace="Microsoft.SharePoint.ApplicationPages"
    Assembly="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace= "Microsoft.SharePoint.ApplicationPages" %>
<%@ Import Namespace= "System.Data" %>
<%@ Import Namespace= "System.IO" %>
<%@ Import Namespace= "System.Xml" %>
<%@ Import Namespace= "System.Xml.Serialization" %>
<%-- 
   // Forms Based Authentication User and Role Management - Attribution notice
   // Author(s):
   //
   //      Edin Kapic (ekapic@gmail.com, http://edinkapic.blogspot.com)
   //
   // This file is distributed under the terms of the Microsoft Community 
   // License (Ms-CL). See http://www.codeplex.com/features/Project/License.aspx
   // for the complete terms of use. 
   //
   // For any distribution that contains code from this file, this notice of 
   // attribution must remain intact, and a copy of the license must be 
   // provided to the recipient.
--%>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    <SharePoint:EncodedLiteral ID="PageTitle"
						Text="Import Forms Based Authentication Roles"
						EncodeMethod="HtmlEncode" runat="server"/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
    <SharePoint:EncodedLiteral ID="TitleArea"
						Text="Import Forms Based Authentication Roles"
						EncodeMethod="HtmlEncode" runat="server"/>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
    Use this page to import forms based authentication roles from existing authentication providers. To import users into the default role provider, choose the appropiate role provider from the toolbar, select the roles you wish to import and click Import Selected Roles button.
	  <wssuc:ToolBar id="onetidNavNodesTB" runat="server">
		<Template_Buttons>
		  <wssuc:ToolBarButton runat="server"
						Text="Import Selected Roles"
						id="idImportUsers"
						ToolTip="New Role"
						OnClick="ImportRolesEventHandler" 
						ImageUrl="/_layouts/images/newitem.gif"
						AccessKey="I"/>		    
		</Template_Buttons>
		
<%--	
        This has been removed to avoid confusion.  Typically a site will only have one provider.
        This user control allows you to manage more than one provider.
--%>        
        <Template_RightButtons>
            <wssawc:EncodedLiteral runat="server" Text="Choose the source role provider: "></wssawc:EncodedLiteral>
		    <asp:DropDownList id="ddlProviders" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="ddlProviders_SelectedIndexChanged" />
		</Template_RightButtons>  
	  </wssuc:ToolBar>

</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <table border="0" cellpadding="10" cellspacing="10">
        <tr>
            <td>
            <h3 class="ms-standardheader"><SharePoint:EncodedLiteral ID="HeaderText" runat="server" text="Roles" EncodeMethod='HtmlEncode'/> &nbsp;</h3>
            <wssawc:MenuTemplate ID="UserMenu" runat="server">
                <wssawc:MenuItemTemplate ID="Details" runat="server" Text="Edit" ImageUrl="/_layouts/images/EDITITEM.GIF" ClientOnClickNavigateUrl="UserEdit.aspx?UserName=%USERNAME%" Title="Edit"></wssawc:MenuItemTemplate>
                <wssawc:MenuItemTemplate ID="Delete" runat="server" Text="Delete" ImageUrl="/_layouts/images/delete.gif" ClientOnClickNavigateUrl="UserDelete.aspx?UserName=%USERNAME%" Title="Delete"></wssawc:MenuItemTemplate>
            </wssawc:MenuTemplate>
                <wssawc:SPGridView ID="MemberGrid" DataKeyNames="Name" AutoGenerateColumns="false"
                    CssClass="ms-vb2" GridLines="None" HeaderStyle-HorizontalAlign="Left" runat="server"  
                    AllowPaging="true" PagerStyle-CssClass="ms-bottompaging" PagerStyle-HorizontalAlign="Center"
                    PageSize="5" PagerSettings-Mode="NextPreviousFirstLast" >

                    <Columns>
                         <asp:TemplateField HeaderText="Select">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkSelect" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>                       
                        <wssawc:SPBoundField HeaderText="Role name" DataField="Name" />
                        <wssawc:SPBoundField DataField="Users" HeaderText="Number of users" >
                            <HeaderStyle Width="200px" />
                        </wssawc:SPBoundField>
                    </Columns>
                    <HeaderStyle HorizontalAlign="Left" />
                    <AlternatingRowStyle CssClass="ms-alternating" />
                    <PagerSettings Mode="NumericFirstLast" />
                    <SelectedRowStyle CssClass="ms-selectednav" Font-Bold="True" />
                    <PagerStyle CssClass="ms-bottompaging" />
                    
                </wssawc:SPGridView>
                <wssawc:SPGridViewPager GridViewId="MemberGrid" runat="server" OnClickNext="MemberGrid_ClickNext" OnClickPrevious="MemberGrid_ClickPrevious" >
                </wssawc:SPGridViewPager>
            </td>
        </tr>
    </table>
</asp:Content>



<script runat="server">
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //if you want to manage more than one provider uncoment this code and the user control above.
        // this.appNameSelector.SelectionMade += new EventHandler(appNameSelector_SelectionMade);

        if (!Page.IsPostBack)
        {
            FillProviders();
            FillGrid();
        }
    }

    void ddlProviders_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillGrid();
    }

    protected void ImportRolesEventHandler(Object sender, EventArgs e)
    {
        RoleProvider importProvider = Roles.Providers[ddlProviders.SelectedValue];

        foreach (GridViewRow gvr in MemberGrid.Rows)
        {
            CheckBox chkSelect = (CheckBox) gvr.FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                string rolename = ((DataKey)MemberGrid.DataKeys[gvr.RowIndex]).Value.ToString();
                
                if (!Roles.RoleExists(rolename))
                {
                    Roles.CreateRole(rolename);
                }
            }
        }

    }

    protected void FillProviders()
    {
        // --- Get the role providers and exclude the default one
        foreach (RoleProvider mp in Roles.Providers)
        {
            // --- Exclude Windows Token role provider and the default role provider
            if(!(mp is WindowsTokenRoleProvider) && Roles.Provider.Name != mp.Name)
            {
                ddlProviders.Items.Add(mp.Name);
            }
        }

    }
    
    protected void MemberGrid_ClickNext(Object sender, EventArgs e)
    {
        FillGrid();
    }

    protected void MemberGrid_ClickPrevious(Object sender, EventArgs e)
    {
        FillGrid();
    }
        
    protected void appNameSelector_SelectionMade(object sender, EventArgs e)
    {
        FillGrid();
    }

    protected void FillGrid()
    {
        DataTable dt = new DataTable("Roles");
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Users", typeof(int));

        if (ddlProviders.Items.Count > 0)
        {
            RoleProvider importProvider = Roles.Providers[ddlProviders.SelectedValue];
            string[] roles = importProvider.GetAllRoles();
            foreach (string role in roles)
            {
                dt.Rows.Add(role, importProvider.GetUsersInRole(role).Length);
            }
        }
        MemberGrid.DataSource = dt;
        MemberGrid.DataBind();
    }
    
    
</script>

